package pl.org.lutra.planner.gui.toolbar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.pushingpixels.flamingo.api.ribbon.JRibbon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority;
import org.pushingpixels.flamingo.api.ribbon.RibbonTask;
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
import org.pushingpixels.flamingo.api.ribbon.resize.RibbonBandResizePolicy;

import pl.org.lutra.planner.gui.main.InputPanel;
import pl.org.lutra.planner.gui.resources.Text;

public class PlannerToolbar extends JRibbon {
    
    private static final long serialVersionUID = -3208328682624517102L;
    private InputPanel inputPanel;
    
    public PlannerToolbar() {
        init();
    }
    
    private void init() {
        JRibbonBand diveBand = createBand("toolbar.band.dive.title");
        
        ToolbarButton addSegmentButton = new ToolbarButton("toolbar.button.addSegment") {
            private static final long serialVersionUID = -4910608968850117530L;
            
            {
                addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        inputPanel.addDiveSegment();
                    }
                });
            }
        };
        diveBand.addCommandButton(addSegmentButton, RibbonElementPriority.TOP);
        
        ToolbarButton deleteSegmentButton = new ToolbarButton("toolbar.button.deleteSegment") {
            private static final long serialVersionUID = 562929426173082845L;
            
            {
                addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        inputPanel.deleteDiveSegment();
                    }
                });
            }
        };
        diveBand.addCommandButton(deleteSegmentButton, RibbonElementPriority.TOP);
        
        JRibbonBand calculationBand = createBand("toolbar.band.calculation.title");
        
        CalculateButton calculateButton = new CalculateButton();
        calculationBand.addCommandButton(calculateButton, RibbonElementPriority.TOP);
        
        RibbonTask planTask = new RibbonTask(Text.get("toolbar.task.plan.title"), diveBand, calculationBand);
        
        addTask(planTask);
    }
    
    private JRibbonBand createBand(String titleKey) {
        JRibbonBand band = new JRibbonBand(Text.get(titleKey), null);
        
        List<RibbonBandResizePolicy> resizePolicies = new ArrayList<RibbonBandResizePolicy>();
        resizePolicies.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(resizePolicies);
        
        return band;
    }
    
    public void setInputPanel(InputPanel inputPanel) {
        this.inputPanel = inputPanel;
    }
    
}
